<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Menu extends Model
{
    /**
     * 模型属性
     * @var string
     */
    protected   $table = 'menu';//表名和模型名称不对应时可设置，切小写
//    protected   $primaryKey = 'id';//主键名称不是id时，可设置
//    public      $incrementing = false;//主键不是自增的话可设置
//    protected   $keyType = 'string';//主键不是整形时可设置
//    public      $timestamps = false;//created_at、updated_at两个字段不存在，或者只有一个可设置，或者通过一下在设置
//
//    public      const CREATED_AT = 'create_time';
//    public      const UPDATED_AT = 'update_time';
//
//    protected   $dateFormat = 'U';//自定义时间类型 unix时间
//    protected   $connection = "";//指定数据库连接

    /**
     * 模型字段
     */
    public      $id;
    public      $title='title';
    public      $parent_id='parent_id';
    public      $psort='psort';
    public      $status='status';
    public      $icon='icon';
    public      $menu_route='menu_route';
    public      $routes='routes';
    public      $description='description';
    public      $created_at='created_at';
    public      $updated_at='updated_at';

    /**
     * 模型实例化属性
     * @var
     */
    public      $model;

    /**
     * 初始化实例化模型
     */
    public function __construct()
    {
        $this->model = DB::table($this->table);
    }

    /**
     * 查询菜单
     * @param $params
     * @return array
     */
    public function getList($params){
      //  $params['where']['disabled']=$params['where']['disabled'] ? $params['where']['disabled'] : 'false';
        if($params['nocount']!==true){
            $data['total'] = $this->model::where($params['where'])->count();
        }
        if($params['getfield']){
            $data['row'] =  $this->model->where($params['where'])->orderBy($params['order'])->select($params['field'])->get()->toArray();
        }else{
            $data['row'] = $this->model::where($params['where'])->select($params['field'])->orderBy($params['order'])->offset($params['page'])->limit($params['count'])->get();
        }
        return !empty($data) ? $data : array();
    }
    /**
     * 添加菜单
     * @param $data
     * @return bool
     */
    public function addMenu($data){
        $params = [
            $this->title    =>   $data['title'],
            $this->parent_id =>   $data['parent_id'],
            $this->psort     =>   $data['psort'],
            $this->status    =>   $data['status'],
            $this->icon      =>   !empty($data['icon']) ? $data['icon'] : '',
            $this->menu_route=>   $data['menu_route'],
            $this->routes    =>   $data['routes'],
            $this->description=>  $data['description'],
            $this->created_at =  date('Y-m-d H:i:s',time()),
            $this->updated_at =  date('Y-m-d H:i:s',time())
        ];
        $rs = $this->model->insert($params);
        return $rs;
    }

    /**
     * 更新菜单
     * @param $where
     * @param $data
     * @return int
     */
    public function updateMenu($where,$data){
        $rs = DB::table($this->table)->where('id', $where)->update($data);
        return $rs;
    }

    /**
     * 删除
     * @param $where
     * @param string $condition
     * @return int
     */
    public function delMenu($where,$condition='single'){
        if($condition=='single'){
            $rs = $this->model::where($where)->first();
        }else{
            $rs = $this->model::destroy($where);
        }
        return $rs;
    }

    /**
     * 查询单条记录
     * @param $where
     * @return Model|\Illuminate\Database\Query\Builder|object|null
     */
    public function getSingle($where){
       return  $this->model->where($where)->first();
    }


}
